#!/bin/bash
#为DB增加实例，一次执行可以增加多个实例

HAS_ERROR=0
function DO_CMD() {
    echo Exec command: $@
    $@
    EXIT_CODE=$?
    if [ $EXIT_CODE != 0 ]; then
        HAS_ERROR=1
        echo ERROR: Execute $@ failed.
        exit $EXIT_CODE
    else
        echo FINE: Execute success.
    fi
}

function SUDO_ORA_CMD() {
    USER=$1
    shift
    CMD_SECTXT=$(echo "$@" | perl -pe 's/Password=\S*/Password=******/g' | perl -pe 's/Password\s+\S*/Password ******/g')
    echo "Exec command: su - '$USER' -c '$CMD_SECTXT'"
    eval "su - '$USER' -c 'AUTOEXEC_JOBID=$AUTOEXEC_JOBID $@'"
    exitCode=$?
    if [ $exitCode -ge 128 ]; then
        #负数返回码，大于127就是负数的补码
        echo "ERROR: Execute command failed, exitCode:$exitCode."
        exit $exitCode
    elif [ $exitCode -gt 0 ]; then
        echo "WARN: Execute command  has some warning, exitCode:$exitCode."
    else
        echo "FINE: All operations were successful."
    fi
}

function SUDO_CMD_CONT() {
    USER=$1
    shift
    echo "Exec command: su - '$USER' -c '$@'"
    eval "su - '$USER' -c 'AUTOEXEC_JOBID=$AUTOEXEC_JOBID $@'"
    EXIT_CODE=$?
    if [ $EXIT_CODE != 0 ]; then
        HAS_WARN=1
        echo WARN: Execute $@ failed.
    else
        echo FINE: Execute success.
    fi
}

#参数处理,全部用长参数
######################################
function parseOpts() {
    OPT_SPEC=":h-:"
    while getopts "$OPT_SPEC" optchar; do
        case "${optchar}" in
        -)
            case "${OPTARG}" in
            CV_ASSUME_DISTID)
                CV_ASSUME_DISTID="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            ORACLE_USER)
                ORACLE_USER="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            NODE_NAMES)
                NODE_NAMES="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            DB_UNIQUE_NAME)
                DB_UNIQUE_NAME="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            SYSDBA_USER)
                SYSDBA_USER="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            SYSDBA_PWD)
                SYSDBA_PWD="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            *)
                if [ "$OPTERR" = 1 ] && [ "${OPT_SPEC:0:1}" != ":" ]; then
                    echo "Unknown option --${OPTARG}" >&2
                fi
                ;;
            esac
            ;;
        h)
            usage
            exit 2
            ;;
        *)
            if [ "$OPTERR" != 1 ] || [ "${OPT_SPEC:0:1}" = ":" ]; then
                echo "Non-option argument: '-${OPTARG}'" >&2
            fi
            ;;
        esac
    done
}

parseOpts "$@"

if [ -z "$ORACLE_USER" ]; then
    ORACLE_USER=grid
fi

ORACLE_UID=$(id -u $ORACLE_USER 2>/dev/null)
if [ -z "$ORACLE_UID" ]; then
    echo "WARN: User $ORACLE_USER not exists, maybe already uninstalled."
fi

ENV_DISTID=""
if [ "$CV_ASSUME_DISTID" != "AUTO" ]; then
    ENV_DISTID="CV_ASSUME_DISTID=$CV_ASSUME_DISTID"
fi

oraEnv=$(SUDO_ORA_CMD $ORACLE_USER env | grep ORACLE_)
echo "$oraEnv"
eval "$oraEnv"

#========开始执行扩展DB节点的操作============
for GDB_NAME in $(echo $DB_UNIQUE_NAME | sed 's/,/ /'); do
    for NODE_NAME in $(echo $NODE_NAMES | sed 's/,/ /'); do
        echo "Try to execute dbca command to add instance $GDB_NAME(node:$NODE_NAME) to cluster..."
        SUDO_ORA_CMD $ORACLE_USER $ENV_DISTID $ORACLE_HOME/bin/dbca -silent -addInstance \
            -gdbName "$GDB_NAME" \
            -nodeName "$NODE_NAME"
        # -sysDBAUserName "$SYSDBA_USER" \
        # -sysDBAPassword "$SYSDBA_PWD"
    done
done

exit $HAS_ERROR
